import {createRouter, createWebHashHistory} from "vue-router";
import {getToken} from "@/util/LocalUtils.js";
import {isWap} from "@/util/BrowserTypeUtils.js";


const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {name: "Home", path: "/home", component: () => import("@/views/home.vue")},
        {name: "Mini", path: "/mini", component: () => import("@/views/mini.vue"), children: [
                {name: "Session", path: "/mini/session", component: () => import("@/views/chat-session.vue")},
                {name: "Input", path: "/mini/chat", component: () => import("@/views/chat-input.vue")},
            ]},
        {name: "Login", path: "/login", component: () => import("@/views/login.vue")}
    ]
})

router.beforeEach((to, from, next) => {
    const token = getToken()
    if (!token) {
        if (to.path !== '/login') {
            next({path: '/login'})
        } else {
            next()
        }
    } else if (isWap() && !to.path.startsWith('/mini')) {
        next({path: '/mini/session'})
    } else if (!isWap() && to.path !== '/home') {
        next({path: '/home'})
    } else {
        next()
    }
})

export default router